const req = require('request');
var config = require('../../config');
const appUtil = require('../../common/utils/appUtils');
const model = require('../models/CommentModel');

module.exports = {
    /**
     * 跳转到新建评论
     * @param req
     * @param res
     */
    getNewComment:function (req, res) {
        // let mid = appUtil.validParams(req.query.mid,'BESTV150625144819000001');

        var mid ='BESTV150625144819000001'
        // var open_id ='oSx2ps7hAjniYA1eKD5c5YdCLBco';

        // var mid =''
        if(req.query.mid){
            mid = req.query.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        res.render('newcomment',{mid:mid,openid:open_id})


    },
    /**
     * 获得更多评论页
     * @param req
     * @param res
     */
    getMoreComment:function (req, res) {
        // var mid ='BESTV150625144819000001'
        // var open_id ='oSx2ps7hAjniYA1eKD5c5YdCLBco';
        var page=req.query.page;
        // var mname=req.query.mname;

        var mid =''
        if(req.query.mid){
            mid = req.query.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };

        model.postCommentListNew([mid,page,open_id],function (reslut) {
            //获得更多评论
            res.render('morecomment',{mid:mid,openid:open_id,comments:reslut.data.rows})
        })
    },
    /**
     * 添加评论
     * @param req
     * @param res
     */
    postaddcomment:function (req, res) {
        var mid =''
        if(req.body.mid){
            mid = req.body.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        var text=req.body.text;
        var num=req.body.num;

        model.postaddcomment([mid,open_id,text,num],function (result) {
            res.json(result);
        })

    },

    postCommentList:function (req, res) {
        //获得mid
        let mid = appUtil.validParams(req.body.mid,'BESTV150625144819000001');

        model.postCommentList(mid,function (result) {
            res.json(result)
        })
    },

    /**
     * 评论分页获取
     * @param req
     * @param res
     */
    postCommentListByPage:function (req, res) {
        //获得mid
        let mid = appUtil.validParams(req.body.mid,'BESTV150625144819000001');
        let lastIndex = parseInt(appUtil.validParams(req.body.lastIndex,10));
        var open_id ='oSx2ps7hAjniYA1eKD5c5YdCLBco';

        // var mid =''
        if(req.query.mid){
            mid = req.query.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        model.postCommentList([mid,lastIndex,open_id],function (result) {
            res.json(result)
        })
    },
    postComment:function (req, res) {
        let score=appUtil.validParams(req.body.score,0);
        let mid=appUtil.validParams(req.body.score,0);
        let comment=appUtil.validParams(req.body.comment,0);
        let openid=appUtil.validParams(req.body.openid,0);
        let date=new Date()

        model.postComment([openid,comment,mid,date,score],function (result) {
            res.json(result)
        })
    },

    /**
     * 评论赞同
     * @param req
     * @param res
     */
    postCommentAgree:function (req, res) {
        let cid=appUtil.validParams(req.body.id,0);
        let agree=parseInt(appUtil.validParams(req.body.agree,0));
        let openid=appUtil.validParams(req.body.open_id,0);
        model.postCommentAgree([cid,openid,agree],function (result) {
            res.json(result)
        })



    },

    getreplycomment:function (req, res) {
        var cid =req.query.cid;
        var name=req.query.mname;
        // var open_id ='oSx2ps7hAjniYA1eKD5c5YdCLBco';

        var mid =''
        if(req.query.mid){
            mid = req.query.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        res.render('replycomment',{cid:cid,openid:open_id,name:name})
    },
    /**
     * 添加评论
     * @param req
     * @param res
     */
    postreplycomment:function (req, res) {
        var mid =''
        if(req.body.mid){
            mid = req.body.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        var text=req.body.text;
        var cid=req.body.cid;

        model.postreplycomment([mid,open_id,text,cid],function (result) {
            res.json(result);
        })

    },

};




